require('../tickets/page.css');
require('./page.less');
const Base64 = require('js-base64').Base64;
const cp = require('cp');
let orderno;
cp.initPage();
require('router/mmRouter.js');
function add_zero(m) {
  return m < 10 ? '0' + m : m;
};
function time(time_num) {
  let time = new Date(time_num);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let hour = time.getHours();
  let minute = time.getMinutes();
  return year + '年' + add_zero(month) + '月' + add_zero(date) + '号 ' + add_zero(hour) + ':' + add_zero(minute);
}
function time_en(time_num) {
  let time = new Date(time_num);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let hour = time.getHours();
  let minute = time.getMinutes();
  let seconds = time.getSeconds();
  return year + '/' + add_zero(month) + '/' + add_zero(date) + ' ' + add_zero(hour) + ':' + add_zero(minute) + ':' + add_zero(seconds);
}
let moduleExports = avalon.define({
  $id: 'mc_orderdetail',
  orderinfo: {},
  enrollinfo: {},
  actinfo: {},
  addressinfo: {
    street: '',
  },
  billinfo: 0,
  contact_show: true,
  invoice_show: false,
  getTicketStatus: status => {
    switch (status) {
      case 1:
        return '待出货';
      case 2:
        return '出货失败';
      case 3:
        return '已出货';
    }
  },
  getCreate: e => {
    return time_en(e);
  },
  getTime: (stime, etime) => {
    let start = time(stime);
    let end = time(etime);
    return start + ' 至 ' + end;
  },
  getInvoice: billinfo => {
    if (billinfo !== 0) {
      const page = require('./invoice.html');
      return page;
    } else {
      return '';
    }
  },
  getOrderStatus: (isopen, paystatus) => {
    if (isopen !== 1) {
      return '订单已关闭';
    } else {
      switch (paystatus) {
        case 1:
          return '未支付';
        case 2:
          return '已支付';
        case 3:
          return '退款中';
        case 4:
          return '退款失败';
        case 5:
          return '退款成功';
      }
    }
  },
  bindCancelOrder: () => {
    cp.ajaxUrl({
      controller: 'goods',
      cmd: 'com.rel.porg.pb.cancel_goods_order_req',
      data: {
        orderno: orderno,
      },
      success: res => {
        moduleExports.orderinfo.isopen = -1;
      },
    });
  },
  validate_contact: {
    onValidateAll: reasons => {
      if (moduleExports.contact_show === false) {
        if (reasons.length) {
          cp.layerMsg({
            msg: reasons[0].getMessage(),
          });
        } else {
          cp.ajaxUrl({
            controller: 'goods',
            cmd: 'com.rel.porg.pb.act_enroller_info_req',
            data: {
              req: 1,
              actenroller: {
                id: moduleExports.enrollinfo.id,
                name: moduleExports.enrollinfo.name,
                mobile: moduleExports.enrollinfo.mobile,
                company: moduleExports.enrollinfo.company,
                title: moduleExports.enrollinfo.title,
              },
            },
            success: res => {
              cp.layerMsg({
                msg: '保存成功',
              });
              moduleExports.contact_show = !moduleExports.contact_show;
            },
          });
        }
      } else {
        moduleExports.contact_show = !moduleExports.contact_show;
      }
    },
  },
  validate_invoice: {
    onValidateAll: reasons => {
      if (moduleExports.invoice_show === false) {
        if (reasons.length) {
          cp.layerMsg({
            msg: reasons[0].getMessage(),
          });
        } else {
          let data;
          if (moduleExports.billinfo.receive === 1) {
            data = {
              req: 1,
              goodsorderbill: moduleExports.billinfo,
            };
          } else {
            data = {
              req: 1,
              address: moduleExports.addressinfo,
              goodsorderbill: moduleExports.billinfo,
            };
          }
          cp.ajaxUrl({
            controller: 'goods',
            cmd: 'com.rel.porg.pb.goods_oder_bill_req',
            data: data,
            success: res => {
              cp.layerMsg({
                msg: '保存成功',
              });
              moduleExports.invoice_show = !moduleExports.invoice_show;
            },
          });
        }
      } else {
        moduleExports.invoice_show = !moduleExports.invoice_show;
      }
    },
  },
});
avalon.router.add('/:id', function(id) {
  let o = "<%=session.getAttribute('orderno')%>";
  console.log(o);
  orderno = Base64.decode(id);
  cp.ajaxUrl({
    // 获取活动基本信息
    controller: 'shopskt',
    cmd: 'com.rel.porg.pb.shop_sel_order_req',
    async: false,
    data: {
      req: 1,
      orderno: orderno,
    },
    success: res => {
      moduleExports.orderinfo = res.orderinfo;
      moduleExports.actinfo = res.tempinfo;
      if (!res.contactinfo.title) {
        res.contactinfo.title = '';
      }
      moduleExports.enrollinfo = res.contactinfo;
      if (res.billinfo) {
        delete res.billinfo.createtime;
        delete res.billinfo.updatetime;
        if (res.billinfo.receive === 1) {
          // 现场领取
          res.billinfo.postcode = '';
          res.billinfo.name = '';
          res.addressinfo = {
            street: '',
          };
          res.billinfo.mobile = '';
        } else {
          // 快递
          moduleExports.addressinfo = res.addressinfo;
        }
        moduleExports.billinfo = res.billinfo;
      }
    },
    fail: () => {
      alert('活动不存在');
    },
  });
});

// 启动路由监听
avalon.history.start({
  root: '/act/index/index/page.html',
  hashPrefix: '',
});
